package org.agilelang.tool.generatordoc;

/*
 AgileLang - A fourth generation language(http://www.agilelang.org).

 Copyright (C) 2013 Roderick Cusirramos (roderick20@hotmail.com)

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.io.*;
import org.agilelang.mvc.Facade;
import org.agilelang.object.Controller;
import org.agilelang.object.Model;
import org.agilelang.object.Project;
import org.agilelang.object.attributes.Attribute;
import org.agilelang.object.attributes.Function;
import org.agilelang.object.attributes.Reference;
import org.agilelang.object.attributes.View;
import word.api.interfaces.IDocument;
import word.w2004.Document2004;
import word.w2004.elements.*;
import word.w2004.elements.tableElements.TableEle;
import word.w2004.style.Font;

/**
 *
 * @author Roderick
 */
public class GeneratorDoc extends Facade {

    String outFile;
    Project project;

    public GeneratorDoc(String path_source) {

        this.path_xml = path_source+ File.separator +"out" +File.separator + "meta.xml";
        this.outFile = path_source+ File.separator +"out" +File.separator + "doc.doc";
        project = this.readProject();

        IDocument myDoc = new Document2004();
        myDoc.encoding(Document2004.Encoding.UTF_8);
        //-- cabecera
        myDoc.getHeader().addEle(
                Paragraph.withPieces(
                ParagraphPiece.with(project.application.name).withStyle().bold().create(), ParagraphPiece.with(" Version :" + project.application.version)).create());

        //-- caratula
        myDoc.addEle(BreakLine.times(15).create());
        myDoc.addEle(Paragraph.withPieces(ParagraphPiece.with(project.application.name).withStyle().font(Font.COURIER).bold().italic().fontSize("35").create()));
        myDoc.addEle(Paragraph.withPieces(ParagraphPiece.with("Autor : " + project.application.author).withStyle().fontSize("25").create()));
        myDoc.addEle(Paragraph.withPieces(ParagraphPiece.with("Version : " + project.application.version).withStyle().fontSize("25").create()));
        myDoc.addEle(PageBreak.create());

        //-- Application        
        myDoc.addEle(Heading1.with("Aplicacion").create());
        myDoc.addEle(Paragraph.with("Nombre de la Aplicacion :" + project.application.name).create());
        myDoc.addEle(Paragraph.with("Autor : " + project.application.author).create());
        myDoc.addEle(Paragraph.with("Version : " + project.application.version).create());
        myDoc.addEle(Paragraph.with("Descripcion : " + project.application.description).create());
        myDoc.addEle(PageBreak.create());

        //-- ControladoresController controller = project.controllers.get(i);
        myDoc.addEle(Heading1.with("Controladores").create());
        Table tbl = new Table();
        tbl.addTableEle(TableEle.TH, "Nombre", "Tipo", "Descripcion");
        tbl.setRepeatTableHeaderOnEveryPage();
        int t = project.controllers.size();
        for (int i = 0; i < t; i++) {
            Controller controller = project.controllers.get(i);
            tbl.addTableEle(TableEle.TD, controller.name, /*controller.type*/ "", controller.description);
        }
        myDoc.addEle(tbl);
        myDoc.addEle(PageBreak.create());

        //-- Modelos
        myDoc.addEle(Heading1.with("Modelos").create());
        tbl = new Table();
        tbl.addTableEle(TableEle.TH, "Nombre", "Descripcion");
        tbl.setRepeatTableHeaderOnEveryPage();
        t = project.models.size();
        for (int i = 0; i < t; i++) {
            Model model = project.models.get(i);
            tbl.addTableEle(TableEle.TD, model.name, model.description);
        }
        myDoc.addEle(tbl);
        myDoc.addEle(PageBreak.create());

        //-- Scaffolds
        /*myDoc.addEle(Heading1.with("Scaffold").create());
         tbl = new Table();
         tbl.addTableEle(TableEle.TH, "Nombre", "Descripcion");
         tbl.setRepeatTableHeaderOnEveryPage();
         t = project.scaffolds.size();
         for (int i = 0; i < t; i++) {
         Scaffold scaffold = project.scaffolds.get(i);
         tbl.addTableEle(TableEle.TD, scaffold.name, scaffold.description);
         }
         myDoc.addEle(tbl);
         myDoc.addEle(PageBreak.create());*/
        //----------------------------------------------------------------------
        //-- Detalle Controlador
        t = project.controllers.size();
        for (int i = 0; i < t; i++) {
            Controller controller = project.controllers.get(i);

            myDoc.addEle(Heading1.with("Controlador : " + controller.name).create());
            myDoc.addEle(Paragraph.with("Descripcion : " + controller.description).create());
            myDoc.addEle(Paragraph.with("Tipo : " + /*controller.type*/ "").create());
            myDoc.addEle(BreakLine.times(1).create());

            myDoc.addEle(Heading2.with("Atributos").create());
            tbl = new Table();
            tbl.addTableEle(TableEle.TH, "Nombre", "Descripcion");
            tbl.setRepeatTableHeaderOnEveryPage();
            /*int tt = controller.attributes.size();
             for (int ii = 0; ii < tt; ii++) {
             Attribute attr = controller.attributes.get(ii);
             tbl.addTableEle(TableEle.TD, attr.name, attr.description);
             }
             myDoc.addEle(tbl);*/

            myDoc.addEle(BreakLine.times(1).create());
            myDoc.addEle(Heading2.with("Vistas").create());
            tbl = new Table();
            tbl.addTableEle(TableEle.TH, "Nombre", "Descripcion");
            tbl.setRepeatTableHeaderOnEveryPage();
            int tt = controller.views.size();
            for (int ii = 0; ii < tt; ii++) {
                View view = controller.views.get(ii);
                tbl.addTableEle(TableEle.TD, view.name, view.description);
            }
            myDoc.addEle(tbl);

            myDoc.addEle(BreakLine.times(1).create());
            myDoc.addEle(Heading2.with("Funciones").create());
            tbl = new Table();
            tbl.addTableEle(TableEle.TH, "Nombre", "Descripcion");
            tbl.setRepeatTableHeaderOnEveryPage();
            tt = controller.functions.size();
            for (int ii = 0; ii < tt; ii++) {
                Function function = controller.functions.get(ii);
                tbl.addTableEle(TableEle.TD, function.name, function.description);
            }
            myDoc.addEle(tbl);
            myDoc.addEle(PageBreak.create());
        }
        //----------------------------------------------------------------------
        //-- Detalle Modelo
        t = project.models.size();
        for (int i = 0; i < t; i++) {
            Model model = project.models.get(i);

            myDoc.addEle(Heading1.with("Modelo : " + model.name).create());
            myDoc.addEle(Paragraph.with("Descripcion : " + model.description).create());

            myDoc.addEle(BreakLine.times(1).create());
            myDoc.addEle(Heading2.with("Atributos").create());
            tbl = new Table();
            tbl.addTableEle(TableEle.TH, "Nombre", "Tipo de Dato", "Nulo", "Descripcion");
            tbl.setRepeatTableHeaderOnEveryPage();
            int tt = model.attributes.size();
            for (int ii = 0; ii < tt; ii++) {
                Attribute attr = model.attributes.get(ii);
                tbl.addTableEle(TableEle.TD, attr.name, attr.type, attr.nullable, attr.description);
            }
            myDoc.addEle(tbl);

            myDoc.addEle(BreakLine.times(1).create());
            myDoc.addEle(Heading2.with("Relaciones").create());
            tbl = new Table();
            tbl.addTableEle(TableEle.TH, "Descripcion", "Key Name", "Foreign Key");
            tbl.setRepeatTableHeaderOnEveryPage();
            tt = model.references.size();
            for (int ii = 0; ii < tt; ii++) {
                Reference ref = model.references.get(ii);
                tbl.addTableEle(TableEle.TD, ref.description, ref.name_key, ref.foreign_key);
            }
            myDoc.addEle(tbl);
        }
        //----------------------------------------------------------------------
        //-- Detalle Scaffold
        /*t = project.scaffolds.size();
         for (int i = 0; i < t; i++) {
         Scaffold scaffold = project.scaffolds.get(i);

         myDoc.addEle(Heading1.with("Scaffold : " + scaffold.name).create());
         myDoc.addEle(Paragraph.with("Descripcion :" + scaffold.description).create());

         myDoc.addEle(BreakLine.times(1).create());
         myDoc.addEle(Heading2.with("Atributos").create());
         tbl = new Table();
         tbl.addTableEle(TableEle.TH, "Nombre", "Tipo de Dato", "Nulo", "Descripcion");
         tbl.setRepeatTableHeaderOnEveryPage();
         int tt = scaffold.attributes.size();
         for (int ii = 0; ii < tt; ii++) {
         Attribute attr = scaffold.attributes.get(ii);
         tbl.addTableEle(TableEle.TD, attr.name, attr.type, attr.notnull, attr.description);
         }
         myDoc.addEle(tbl);

         myDoc.addEle(BreakLine.times(1).create());
         myDoc.addEle(Heading2.with("Relaciones").create());
         tbl = new Table();
         tbl.addTableEle(TableEle.TH, "Descripcion", "Key Name", "Foreign Key");
         tbl.setRepeatTableHeaderOnEveryPage();
         tt = scaffold.references.size();
         for (int ii = 0; ii < tt; ii++) {
         Reference ref = scaffold.references.get(ii);
         tbl.addTableEle(TableEle.TD, ref.description, ref.name_key, ref.foreign_key);
         }
         myDoc.addEle(tbl);

         myDoc.addEle(BreakLine.times(1).create());
         myDoc.addEle(Heading2.with("Vistas").create());
         tbl = new Table();
         tbl.addTableEle(TableEle.TH, "Nombre", "Descripcion");
         tbl.setRepeatTableHeaderOnEveryPage();
         tt = scaffold.views.size();
         for (int ii = 0; ii < tt; ii++) {
         View view = scaffold.views.get(ii);
         tbl.addTableEle(TableEle.TD, view.name, view.description);
         }
         myDoc.addEle(tbl);
         }*/


        //----------------------------------------------------------------------
        String myWord = myDoc.getContent();


        Writer output = null;
        File file = new File(outFile);
        try {
            output = new BufferedWriter(new FileWriter(file));
            output.write(myWord);
            output.close();
        } catch (IOException ex) {
            System.out.println(ex.toString());
        }
    }

    public GeneratorDoc() {
        
    }
}
